package com.example.shengyin.controller;


import com.example.shengyin.entity.Admin;
import com.example.shengyin.entity.Event;
import com.example.shengyin.entity.Page;
import com.example.shengyin.service.EvenService;
import com.example.shengyin.util.Result;
import com.example.shengyin.util.StrUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
 
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.validation.BindingResult;
 
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.text.SimpleDateFormat;
import java.util.Date;

@RestController
@RequestMapping("/event")
@Validated
@CrossOrigin(origins = "*", maxAge = 3600,allowCredentials="true")
//swagger
@Api("活动")
public class EventController {

    @Autowired
    private EvenService service;
    @Autowired
    private RedisTemplate redisTemplate;
    @ApiOperation("活动列表")
    @GetMapping("/search")

    public Result event(@ApiParam(name = "title",value = "活动标题查找") @RequestParam(required = false) String title,
                        @RequestParam(name = "page",defaultValue = "1") int page,
                        @RequestParam(name = "limit",defaultValue = "10")int limit) {
        Page p = service.selectList(title, page, limit);
        if(p.getCount()==0){
            return Result.success(p,"没有数据");
        }
        return Result.success(p);
    }

    @GetMapping(value = "/select/{id}")
    public Result select(@PathVariable(value = "id") int id)
    {
        Event select = service.selectById(id);
        if(select==null)
            return Result.error("没有找到数据");
        return Result.success(select);
    }

    @ApiOperation("增加活动列表")
    @PostMapping("/add")
    public Result insert(@Valid @RequestBody Event event, BindingResult bindingResult, HttpServletRequest request) {
        Admin admin = (Admin) redisTemplate.opsForValue().get(request.getHeader("Authorization"));
        event.setPoster(admin.getUname());
        int insert = service.add(event);
        return insert > 0 ? Result.success("添加成功") : Result.error("添加失败");
    }

    @ApiOperation("修改活动列表")
    @PostMapping("/update")
    public Result update(@RequestBody Event event)
    {
        int insert = service.update(event);
        if(insert==0)
            return Result.error("修改失败");
        return Result.success("修改成功");
    }

    @GetMapping("/delete/{id}")
    public Result delete(@PathVariable(value = "id")int id)
    {
        int delete = service.delById(id);
        if(delete==0)
            return Result.error("删除失败");
        return Result.success("删除成功");
    }

    @GetMapping("/deleteByIds/{ids}")
    public Result deleteByIds(@PathVariable(value = "ids")String ids)
    {
        System.out.println("开始这些");
        String s = StrUtil.ListtoString(ids);
        int delete = service.delByIds(s);
        if(delete==0)
            return Result.error("删除失败");
        return Result.success("删除成功");
    }

    @InitBinder// String----->Date
    public void initBinder(WebDataBinder binder) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        dateFormat.setLenient(false);
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
        // true:允许输入空值，false:不能为空值 }
        //特殊的格式
        //SimpleDateFormat d2 = new SimpleDateFormat("yyyy/MM/dd");

        //binder.registerCustomEditor(Date.class,"birthday", new CustomDateEditor(d2, true));

    }


}
